package com.e_library.dao.classes;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.e_library.jpa.entities.Collection;
import com.e_library.jpa.entities.Main;

public class CollectionDaoImpl implements CollectionIDao{

	private EntityManager em;
    public CollectionDaoImpl() {
    	Main m=new Main();
          em=m.em;
          em.getTransaction().begin();
	}



	 public void deleteOne(String id) {
		   
		
      Collection  collection= em.find(Collection.class, id);
	 if (collection == null) {
	throw new DaoException(2);

	}
	em.remove(collection);
	em.getTransaction().commit();

	 
	 }
	
	@SuppressWarnings("unchecked")
	 public List<Collection> getAll() {
	 return em.createQuery("select p from Collection p").getResultList();
	 }
	
	 @SuppressWarnings("unchecked")
	 public List<Collection> getAllLike(String modele) {
	 return em.createQuery("select p from Collection p where p.nom_collection like :modele").setParameter("modele", modele).getResultList();
	 }
	

	 public Collection getOne(String id) {
	 return em.find(Collection.class, id);
	 }
	

	 public Collection saveOne(Collection collection) {
	 em.persist(collection);
	 em.getTransaction().commit();
	 return collection;
  }
	

	 public Collection updateOne(Collection collection) {
	 Collection collection2=em.merge(collection);
	 em.getTransaction().commit();
	 return collection2;

	 }
}
